package com.hospital.controller;

import com.hospital.entity.User;
import com.hospital.entity.UserDepartment;
import com.hospital.service.UserDepartmentService;
import com.hospital.service.UserService;
import com.hospital.util.JsonUtil;
import com.hospital.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户相关操作controller层
 */
@Controller
@CrossOrigin
@RequestMapping("User")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDepartmentService userDepartmentService;
    @Autowired
    private JsonUtil jsonUtil;

    /**
     * 登录方法
     *
     * @param username 用户名
     * @param password 密码
     * @param session  作用域
     * @return 登录信息
     */
    @RequestMapping(value = "Login", produces = "text/application;charset=utf8")
    @ResponseBody
    public String Login(String username, String password, HttpSession session) {
        User user = userService.login(username);
        if (user == null) {
            jsonUtil.setCode("用户名不存在");
            return jsonUtil.getCode();
        } else if (!username.equals(user.getUsername())) {
            jsonUtil.setCode("用户名错误");
            return jsonUtil.getCode();
        } else if (!password.equals(user.getPassword())) {
            jsonUtil.setCode("密码错误");
            return jsonUtil.getCode();
        }
        jsonUtil.setRestate(true);
        //存入用户名
        session.getServletContext().setAttribute("user", user);
        return String.valueOf(jsonUtil.getRestate());
    }

    /**
     * 登出方法
     */
    @RequestMapping("Logout")
    @ResponseBody
    public Boolean Logout(HttpSession session) {
        session.getServletContext().removeAttribute("user");
        String user = (String) session.getServletContext().getAttribute("user");
        return (user == null);
    }

    /**
     * 添加用户方法
     *
     * @param user 用户对象
     * @return 添加状态
     */
    @RequestMapping("Insert")
    @ResponseBody
    public Boolean Insert(User user) {
        user.setDeleteState("1");
        Integer state = userService.Insert(user);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }


    /**
     * 根据用户姓名查询方法
     *
     * @param name 用户姓名
     * @return 用户信息
     */
    @RequestMapping("SelectByUserName")
    @ResponseBody
    public List<Object> SelectByUserName(String name) {
        List<Object> list = new ArrayList<>();
        List<User> users = userService.SelectByUserName(name);
        list.add(users);
        return list;
    }

    /**
     * 修改密码
     *
     * @param newPassword 新密码
     * @param session     作用域
     * @return 修改状态
     */
    @RequestMapping("UpdatePassword")
    @ResponseBody
    public Boolean UpdatePassword(String newPassword, HttpSession session) {
        //获取登录用户名
        User user = (User) session.getServletContext().getAttribute("user");
        System.out.println(user);
        System.out.println(newPassword);
        //修改密码操作
        Integer state = userService.updatePassword(user.getUsername(), newPassword);
        jsonUtil.setRestate(state == 1);
        //返回修改状态
        return jsonUtil.getRestate();
    }

    /**
     * 查单个用户
     *
     * @param session 作用域
     * @return 用户对象
     */
    @RequestMapping("SelectUser")
    @ResponseBody
    public User SelectUser(HttpSession session) {
        //获取登录用户名
        User user = (User) session.getServletContext().getAttribute("user");
        User users = userService.SelectUser(user.getUsername());
        return users;
    }

    /**
     * 根据主键查询用户信息
     *
     * @param id 用户表主键
     * @return 用户信息
     */
    @ResponseBody
    @RequestMapping("SelectByUserId")
    public User SelectById(Integer id) {
        User user = userService.SelectById(id);
        return user;
    }

    /**
     * 修改方法
     *
     * @return 修改状态
     */
    @ResponseBody
    @RequestMapping("Update")
    public Boolean Update(Integer id, String username, String name, String sex, String phone, String email, String password) {
        Integer state = userService.Update(new User(id, username, name, sex, phone, email, password, null, null, null));
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 批量删除用户方法
     *
     * @param id User primary key
     * @return 删除状态
     */
    @RequestMapping("BulkDelete")
    @ResponseBody
    public Boolean BulkDelete(String id) {
        List<Integer> list = new ArrayList<Integer>();
        String[] strs = id.split(",");
        Integer state = 0;
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            state = userService.bulkDelete(list);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 查所有用户
     *
     * @param page    当前页
     * @param pageNum 每页显示条数
     * @return 分页后的用户数据
     */
    @RequestMapping("SelectAll")
    @ResponseBody
    public List<Object> SelectAll(Integer page, Integer pageNum) {
        //查所有数量
        Integer count = userService.SelectAllCount();
        //分页工具
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        //查所有数据
        if (page == null && pageNum == null) {
            pages = page;
            pageNums = pageNum;
        }
        List<User> users = userService.SelectAll(pages, pageNums);
        ArrayList<Object> list = new ArrayList<Object>();
        //查询所有科室
        List<UserDepartment> userDepartments = userDepartmentService.SelectAllDepartment();
        //遍历所有用户
        for (User user : users) {
            //遍历所有用户对应的科室
            for (UserDepartment userDepartment : userDepartments) {
                if (user.getId().equals(userDepartment.getUserId())) {
                    //封装用户对应的科室
                    user.setResponsibleDepartment(userDepartment.getResponsibleDepartments());
                }
            }
        }
        list.add(users);
        list.add(paging);
        return list;
    }

    /**
     * 条件查询
     *
     * @param page    当前页
     * @param pageNum 每页显示条数
     * @param user    当前user对象
     * @return 集合中的user对象
     */
    @RequestMapping("SelectByUserLike")
    @ResponseBody
    public List<Object> SelectByUserLike(Integer page, Integer pageNum, User user) {
        user.setName("%" + user.getName() + "%");
        user.setPhone("%" + user.getPhone() + "%");
        user.setEmail("%" + user.getEmail() + "%");
        user.setPassword("%" + user.getPassword() + "%");
        //条件查询所有数量
        Integer count = userService.SelectByUserLikeCount(user);
        //list集合
        List<Object> list = new ArrayList<>();
        //分页工具类调用方法
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        //当前页
        Integer pages = paging.get("page");
        //每页条数
        Integer pageNums = paging.get("pageNum");
        //查询所有科室
        List<UserDepartment> userDepartments = userDepartmentService.SelectAllDepartment();
        //条件查询方法
        List<User> users = userService.SelectByUserLike(pages, pageNums, user);
        //遍历所有用户
        for (User user1 : users) {
            //遍历所有用户对应的科室
            for (UserDepartment userDepartment : userDepartments) {
                if (user1.getId().equals(userDepartment.getUserId())) {
                    //封装用户对应的科室
                    user1.setResponsibleDepartment(userDepartment.getResponsibleDepartments());
                }
            }
        }
        //添加数据
        list.add(users);
        list.add(paging);
        //设置返回集合数据
        jsonUtil.setResults(list);
        return jsonUtil.getResults();
    }
}